//
//  NovalDataBase.m
//  UI_DataBase
//
//  Created by dllo on 15/8/14.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "NovalDataBase.h"

@implementation NovalDataBase

+(void)insertHolidayWithModel:(DetailShowModel *)model
{
    //  第一步: 获得数据库指针
    sqlite3 *db = [DB openDB];
    
    //  第二步: 获得数据可语句
   NSString *sqlstr = [NSString stringWithFormat:@"insert into holiday (title, address, image, url) values ('%@', '%@', '%@', '%@')", model.title, model.address, model.imageUrlStr, model.urlStr];
    // 第三步: 执行sql语句
    int result = sqlite3_exec(db, [sqlstr UTF8String], nil, nil, nil);
    
    // 第四步: 判读是否为空
    if (result == SQLITE_OK) {
        NSLog(@"插入成功" );
    } else {
        NSLog(@"插入失败" );
    }
}
+(void)updateHolidayWithModel:(DetailShowModel *)model
{
    sqlite3 *db = [DB openDB];
    NSString *upStr =  [NSString stringWithFormat:@"update holiday set address = '%@' where title = '%@'",model.address, model.title];
    int result = sqlite3_exec(db, [upStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    } else {
        NSLog(@"更新失败");
    }
}
+(void)deleteHolidayWithModel:(DetailShowModel *)model
{
    sqlite3 *db = [DB openDB];
    NSString *delStr = [NSString stringWithFormat:@"delete from holiday where title = '%@'", model.title];
    int result = sqlite3_exec(db, [delStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
       NSLog(@"删除失败");
    }
}
+(NSArray *)selectAllHoliday
{
    
    // 查询: 1. 查询所有的 2. 查询单个
    
    // 获得数据库指针
    sqlite3 *db = [DB openDB];
    
    // 定义一个容纳查询结果的变量
    sqlite3_stmt *stmt = nil;
    
    // 获得数据库语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from holiday"];
    
    // 判断sql语句是否正确
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *myTitle = sqlite3_column_text(stmt, 0);
            const unsigned char  *myAddress = sqlite3_column_text(stmt, 1);
            const unsigned char *myImage = sqlite3_column_text(stmt, 2);
            const unsigned char *myurl = sqlite3_column_text(stmt, 3);
            
            NSString *my_title = [NSString stringWithUTF8String:(const char *) myTitle];
            NSString *my_address = [NSString stringWithUTF8String:(const char *)myAddress];
            NSString *my_image = [NSString stringWithUTF8String:(const char *)myImage];
            NSString *my_url = [NSString stringWithUTF8String:(const char *)myurl];
            
            DetailShowModel *model = [DetailShowModel holidayWithTitle:my_title address:my_address image:my_image urlStr:my_url];
              [array addObject:model];
        }
    }
    sqlite3_finalize(stmt);
    return array;
}

+(DetailShowModel *)selectOneWith:(NSString *)title
{
    
    // 查询: 1. 查询所有的 2. 查询单个
    
    // 获得数据库指针
    sqlite3 *db = [DB openDB];
    
    // 定义一个容纳查询结果的变量
    sqlite3_stmt *stmt = nil;
    
    // 获得数据库语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from holiday where title = '%@'", title];
    
    // 判断sql语句是否正确
    int result = sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &stmt, nil);
//    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *myTitle = sqlite3_column_text(stmt, 0);
            const unsigned char  *myAddress = sqlite3_column_text(stmt, 1);
            const unsigned char *myImage = sqlite3_column_text(stmt, 2);
            const unsigned char *myurl = sqlite3_column_text(stmt, 3);
            
            NSString *my_title = [NSString stringWithUTF8String:(const char *) myTitle];
            NSString *my_address = [NSString stringWithUTF8String:(const char *)myAddress];
            NSString *my_image = [NSString stringWithUTF8String:(const char *)myImage];
            NSString *my_url = [NSString stringWithUTF8String:(const char *)myurl];
            DetailShowModel *model = [DetailShowModel holidayWithTitle:my_title address:my_address image:my_image urlStr:my_url];
                sqlite3_finalize(stmt);
            return  model;
        }
    }
    sqlite3_finalize(stmt);
    return nil;
}

@end
